﻿package com.devilcat.ob {
	import flash.display.MovieClip ;
	import flash.events.Event;  
	import flash.display.Loader;
	import flash.net.URLRequest; 
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	import fl.transitions.TweenEvent; 
	import com.devilcat.dt.Gamedt;
	public class NPCMessenger extends MovieClip  {
		public var eventLocation:String="";
		public var eventNumber:Number=0;
		public var eventChange:Boolean=true;
		public var destroy:Boolean=false;
		public var tweenList:Array = new Array();
		public function NPCMessenger(XMLData:XMLList){		
		trace("XMLData.el!=null "+ XMLData.el!=null);
		trace("XMLData.des!=null "+ XMLData.des!=null);
			destroy=false;
			trace("111111");
			trace("222222" +String(XMLData.des)=="");
			if(String(XMLData.des)!="")		
				destroy=true;			
				
			loadIMG(XMLData.img);
			if(XMLData.el!=null) eventLocation = XMLData.el;
			if(XMLData.en!=null) eventNumber = XMLData.en;			
			NPCMessBox.Content.text=String(XMLData.mes);
			NPCMessBox.NPCMessBoxBG.height=20*Number(NPCMessBox.Content.numLines);
			NPCMessBox.y= -44 -Number(NPCMessBox.Content.numLines - 1)*20;
			NPCMessBox.visible=false;
			
			if(String(XMLData.evc)!="")eventChange=false;		
		}
		public function osuper(imgPath:String, d:Boolean=false){
			loadIMG(imgPath);
			destroy=d;
			//this.addEventListener(Event.ENTER_FRAME, checkHit);			
		}
//		function checkHit(e:Event){
//			hitMe();			
//			if(destroy)destroyTween();
//		}
		private function loadIMG(imgPath:String) {
			var imageLoader:Loader = new Loader();
			var imageLink:URLRequest = new URLRequest(imgPath+".png");
			imageLoader.load(imageLink);
			imageLoader.contentLoaderInfo.addEventListener(Event.INIT,imageLoaded);
			function imageLoaded(e:Event):void {
				addChild(imageLoader);
			}
		}
		public function addMe(){
			//if(this.parent != null){
				this.parent.addChild(this);
				//trace ("Removed");
			//}
			tweenList.unshift(new Tween (this,"alpha",Strong.easeOut,0,1,2,true));
			tweenList.unshift(new Tween (this,"y",Strong.easeOut,this.y-50,this.y,2,true));
		}
		public function destroyTween(){
					tweenList.unshift(new Tween (this,"alpha",Strong.easeOut,1,0,2,true));
					tweenList.unshift(new Tween (this,"scaleX",Strong.easeOut,1,2,2,true));
					tweenList.unshift(new Tween (this,"scaleY",Strong.easeOut,1,2,2,true));
					tweenList.unshift(new Tween (this,"y",Strong.easeOut,this.y,this.y-50,2,true));
					tweenList.unshift(new Tween (this,"x",Strong.easeOut,this.x,this.x+220,2,true));
					tweenList[0].addEventListener(TweenEvent.MOTION_FINISH,removeMe);
		}
		public function removeMe(e:Event){
			if(this.parent != null){
				this.parent.removeChild(this);
				//trace ("Removed");
			}
		}
		public function hitMe(){	
			NPCMessBox.visible=true;
			if(eventLocation!="" && eventChange){
				Gamedt.Player.ChangeEvent(eventLocation,eventNumber);
				eventChange=false;				
			}
			if(destroy)destroyTween();
		}
	}
}
	